//
//  SEDoctorDetailServiceButton.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/12.
//

import UIKit

@IBDesignable class SEDoctorDetailServiceButton: UIView {
    
    var tappedClosure: ((Bool) -> Void)?
    
    var view: UIView!
    
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var descLabel: UILabel!
    
    @IBInspectable var image: UIImage? {
        get { imageView.image }
        set { imageView.image = newValue }
    }
    
    @IBInspectable var title: String? {
        get { titleLabel.text }
        set { titleLabel.text = newValue }
    }
    
    @IBInspectable var desc: String? {
        get { descLabel.text }
        set { descLabel.text = newValue }
    }
    
    @IBInspectable var titleColor: UIColor {
        get { titleLabel.textColor }
        set { titleLabel.textColor = newValue }
    }
    
    @IBInspectable var descColor: UIColor {
        get { descLabel.textColor }
        set { descLabel.textColor = newValue }
    }
    
    private var _borderColor = UIColor(hex: "#EDEDED")
    @IBInspectable var borderColor: UIColor {
        get { _borderColor }
        set { _borderColor = newValue }
    }
    
    private var _selectedBorderColor = Asset.main.color
    @IBInspectable var selectedBorderColor: UIColor {
        get { _selectedBorderColor }
        set { _selectedBorderColor = newValue }
    }
    
    private var _isSelected: Bool = false {
        didSet {
            view.layer.borderColor = _isSelected ? selectedBorderColor.cgColor : borderColor.cgColor
        }
    }
    @IBInspectable var isSelected: Bool {
        get { _isSelected }
        set { _isSelected = newValue }
    }
    
    // MARK: - life cycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        view = loadViewFromNib()
        
        view.layer.borderWidth = 1
        view.layer.borderColor = _isSelected ? selectedBorderColor.cgColor : borderColor.cgColor
        view.layer.cornerRadius = view.height / 2
        view.layer.masksToBounds = true
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapped))
        view.addGestureRecognizer(tapGesture)
    }
    
    @objc private func didTapped() {
        guard !isSelected else { return }
        isSelected = !_isSelected
        tappedClosure?(isSelected)
    }
    
    // MARK: - load nib for view
    
    func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName(), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)

        return view
    }

    private func nibName() -> String {
        return String(describing: type(of: self))
    }
}
